package com.springcloud.learn;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.Random;

/**
 * Created by Administrator on 2017/10/29.
 */
@RestController
public class HelloController {

    @Autowired
    private DiscoveryClient discoveryClient;

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String index() throws InterruptedException {
        //测试超时
        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
      //  int sleepTime = new Random().nextInt(2000);
        //System.out.println("sleepTime :"+sleepTime);
        //Thread.sleep(sleepTime);
        System.out.println("/hello ,host:" + instance.getHost() + ",service_id:" + instance.getServiceId() + ",port:" + instance.getPort());
        return "hello word";
    }


    @RequestMapping(value = "/hello1", method = RequestMethod.GET)
    public String hello1(@RequestParam String name) {
        return "hello  " + name;
    }

    @RequestMapping(value = "/hello2",method = RequestMethod.GET)
    public User hello2(@RequestHeader String name,@RequestHeader Integer age){
        return new User(name,age);
    }

    @RequestMapping(value = "/hello3",method = RequestMethod.POST)
    public String hello3(@RequestBody User user){
            return "hello  "+user.getName()+","+user.getAge();

    }

}
